package com.sf.fy.uav.applet.scenicspot;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sf.fy.uav.common.request.operate.MerchantReq;
import com.sf.fy.uav.common.request.operate.OrderCommodityQuery;
import com.sf.fy.uav.common.response.operational.MerchantRes;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.common.utils.PhoneEncoderUtil;
import com.sf.fy.uav.common.utils.RespJsonPageData;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.controller.BusinessRest;
import com.sf.fy.uav.controller.CommodityRest;
import com.sf.fy.uav.controller.OrderCommodityRest;
import com.sf.fy.uav.entity.Business;
import com.sf.fy.uav.entity.Commodity;
import com.sf.fy.uav.entity.OrderCommodity;
import com.sf.fy.uav.order.controller.OrderInfoRest;
import com.sf.fy.uav.order.entity.OrderInfo;
import com.sf.fy.uav.rest.ScenicSpotRest;
import com.sf.fy.uav.withdrawal.entity.ScenicSpot;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ta136817
 */
@RestController
@RequestMapping("/home/merchant/")
@RequiredArgsConstructor
@Api(tags = "商家管理")
@Tag(name = "商家管理", description = "商家管理")
@Log4j
@Slf4j
public class MerchantController {
    private final static Logger logger = LoggerFactory.getLogger(MerchantController.class);

    private final ScenicSpotRest scenicSpotRest;

    private final BusinessRest businessRest;

    private final PhoneEncoderUtil phoneEncoderUtil;

    private final OrderCommodityRest orderCommodityRest;

    private final CommodityRest commodityRest;

    private final OrderInfoRest orderInfoRest;

    private final static Integer CONST_NUM = 0;

    @GetMapping("get-merchant")
    public RespJsonPageData<MerchantRes> getScenicSpot(@RequestParam Integer page, @RequestParam Integer size, @RequestParam(required = false) String name, @RequestParam(required = false) Integer status,@RequestParam(required = false) String brand) {
        MerchantReq merchantReq = new MerchantReq();
        merchantReq.setPage(page);
        merchantReq.setSize(size);
        merchantReq.setName(name);
        merchantReq.setBrand(brand);
        merchantReq.setStatus(status);
        LambdaQueryWrapper<Business> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(name), Business::getName, name)
                .like(StringUtils.isNotBlank(brand), Business::getBrand, brand)
                .eq(!ObjectUtils.isEmpty(status), Business::getStatus, status)
                .orderByDesc(Business::getCreateTime);
        Page<Business> merchantPageList = businessRest.getBusinessesPage(page, size, wrapper);

        if(CollectionUtils.isEmpty(merchantPageList.getRecords())){
            logger.info("查询商家列表"+merchantPageList.getRecords());
            List<MerchantRes> merchantResList = new ArrayList<>();
            return RespJsonPageData.success(merchantResList, CONST_NUM);
        }
        Set<String> businessIdSet = merchantPageList.getRecords().stream().map(Business::getId).collect(Collectors.toSet());
        List<Commodity> commodityList = commodityRest.selectProductByBusinessIds(businessIdSet);
        Set<String> commoditySet = new HashSet<>();
        if(!CollectionUtils.isEmpty(commodityList)){
            commoditySet = commodityList.stream().map(Commodity::getId).collect(Collectors.toSet());
        }
        List<OrderCommodity> orderCommodityList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(commoditySet)){
            orderCommodityList  = orderCommodityRest.getOrderCommodityInCommodityIds(commoditySet);
        }
        Set<String> orderSnSet = new HashSet<>();
        if(!CollectionUtils.isEmpty(orderCommodityList)){
            orderSnSet = orderCommodityList.stream().map(OrderCommodity::getOrderSn).collect(Collectors.toSet());
        }
        List<OrderInfo> orderInfoList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(orderSnSet)){
            orderInfoList = orderInfoRest.getOrderInfoListInTcWaybillNos(orderSnSet);
        }
        if(!CollectionUtils.isEmpty(orderInfoList)){
            Map<String,OrderInfo> waybillNoAndOrderInfoMap = orderInfoList.stream().collect(Collectors.toMap(OrderInfo::getTcWaybillNo, each -> each, (value1, value2) -> value1));
            List<OrderCommodity> orderCommodityNewList = orderCommodityList.stream().filter(orderCommodity -> !ObjectUtils.isEmpty(waybillNoAndOrderInfoMap.get(orderCommodity.getOrderSn()))).collect(Collectors.toList());
            merchantReq.setOrderCommodityNewList(BeanUtil.copyProperties(orderCommodityNewList,OrderCommodityQuery.class));
        }
        return businessRest.getMerchant(merchantReq);
    }

    @GetMapping("get-scenicSpotName")
    public  Rs<Map<String,String>> getScenicSpotName(@RequestParam(required = false) String id, @RequestParam(required = false) String name) {
        return scenicSpotRest.queryScenicSpotName(id,name);
    }
    @GetMapping("get-scenicSpotNameTwo")
    @Operation(tags = "查询景区下拉列表", description = "查询景区下拉列表正常的")
    @CrossOrigin
    public  Rs<List<ScenicSpot>>  getScenicSpotNameTwo(@RequestParam(required = false) String id, @RequestParam(required = false) String name) {
        return scenicSpotRest.queryScenicSpotNameTwo(id,name);
    }

    @GetMapping("get-scenicSpotNameThree")
    @Operation(tags = "查询景区下拉列表", description = "查询景区下拉列表全部的")
    @CrossOrigin
    public  Rs<List<ScenicSpot>>  getScenicSpotNameThree(@RequestParam(required = false) String id, @RequestParam(required = false) String name) {
        return scenicSpotRest.queryScenicSpotNameThree(id,name);
    }
    @PostMapping ("add-merchant")
    public Rs<Integer> addMerchant(@RequestBody MerchantReq merchantReq) {
       return businessRest.addMerchant(merchantReq);
    }
    @PostMapping ("edit-merchant")
    public Rs<Integer> editMerchant(@RequestBody MerchantReq merchantReq) {
        return businessRest.editMerchant(merchantReq);
    }
    @GetMapping("phone-number-decryption")
    public Business getMerchantPhone(@RequestParam String id) {
        Business business = businessRest.getBusinessesByIdAndNoByStatus(id);
        business.setPhone(phoneEncoderUtil.decryptString(business.getPhone()));
        return business;
    }
}
